﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace PivotServerTools
{
    /// <summary>
    /// Pivot settings
    /// </summary>

    public static class SPPivotSettings
    {

        #region Private attributes


        private const string query = "pvt_query";


        private const string maxitems = "pvt_maxitems";


        private const string facetcategories = "pvt_facetcategories";


        private const string xslt = "pvt_xslt";

        private const string defaultimage = "pvt_defaultimage";

        #endregion


        #region Properties

        /// <summary>
        /// Some kind of query used by this collection
        /// </summary>
        public static string Query
        {
            get { return SPContext.Current.Web.AllProperties[query] as string; }
            set { SetProperty(query,value); }
        }

        /// <summary>
        ///  Maximum
        /// </summary>
        public static int? MaxItems
        {
            get { return SPContext.Current.Web.AllProperties[maxitems] as int?; }
            set { SetProperty(maxitems, value == null ? null : value.ToString()); }
        }

        /// <summary>
        /// XML with facet categories description
        /// </summary>
        public static string FacetCategories
        {
            get { return SPContext.Current.Web.AllProperties[facetcategories] as string; }
            set { SetProperty(facetcategories,value); }
        }

        /// <summary>
        /// Xslt transformation rules
        /// </summary>
        public static string Xslt
        {
            get { return SPContext.Current.Web.AllProperties[xslt] as string; }
            set { SetProperty(xslt,value); }
        }

        /// <summary>
        /// Url of default image file
        /// </summary>
        public static string DefaultImage
        {
            get { return SPContext.Current.Web.AllProperties[defaultimage] as string; }

            set { SetProperty(defaultimage,value); }
        }

        private static void SetProperty(string key, string value)
        {
            if (String.IsNullOrEmpty(value))
                return;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Web.Url)){
                    using (SPWeb web = site.OpenWeb()){
                    web.AllowUnsafeUpdates = true;
                    try
                    {
                        if (!web.AllProperties.ContainsKey(key))
                        {
                            web.Properties.Add(key, value);
                            web.Properties.Update();
                        }
                        else
                        {
                            web.AllProperties[key] = value;
                            web.Update();
                        }

                        

                    }
                    finally
                    {
                        web.AllowUnsafeUpdates = false;
                    }
                    }
                }
            });
        }

        #endregion


    }
}
